//
//  MenuCollection.m
//  项目三
//
//  Created by rimi1 on 2016/12/8.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "MenuCollection.h"
#import "MenuCollectionViewCell.h"

static NSString *reusing_ = @"MenuCollectionCell";

@interface MenuCollection () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) UIButton *maskButton;
@property (nonatomic, strong) NSMutableArray <UIColor *> *selectArr;

@end

@implementation MenuCollection

- (instancetype)initWithDataArr:(NSArray *)arr {
    CGRect frame = CGRectMake(0, 0, kWIDTH, kHEIGHT);
    self = [super initWithFrame:frame];
    if (self) {
        NSMutableArray *muArr = [arr mutableCopy];
        for (int i = 0; i < 3; i++) {
            [muArr removeObjectAtIndex:0];
        }
        
        self.dataArr = muArr;
        [self initiaSelectArr];
        [self addSubview:self.maskButton];
        [self addSubview:self.collectionView];
        self.alpha = 0;
    }
    return self;
}

- (void)initiaSelectArr {
    self.selectArr = [NSMutableArray array];
    for (int i = 0; i < self.dataArr.count; i++) {
        [self.selectArr addObject:[UIColor blackColor]];
    }
}

- (void)removeColor {
    [self initiaSelectArr];
    [self.collectionView reloadData];
}

#pragma mark - Animation

- (void)showAnimation {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

- (void)hideAnimation {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - respondsTo.

- (void)respondsToMask:(UIButton *)sender {
    [self hideAnimation];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reusing_ forIndexPath:indexPath];
    NSDictionary *dic = self.dataArr[indexPath.row];
    
    cell.title.text = dic[@"name"];
    cell.title.textColor = self.selectArr[indexPath.row];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self initiaSelectArr];
    self.selectArr[indexPath.row] = [UIColor orangeColor];
    [self.collectionView reloadData];
    [self.delegate menuCollection:self selectOfIndexPath:indexPath];
}

#pragma mark - get

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(WXQAdaptionWidth(80), WXQAdaptionHeight(30));
        layout.minimumLineSpacing = WXQAdaptionHeight(25);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, WXQAdaptionHeight(100), kWIDTH, WXQAdaptionHeight(260)) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[MenuCollectionViewCell class] forCellWithReuseIdentifier:reusing_];
        
    }
    return _collectionView;
}

- (UIButton *)maskButton {
    
    if (!_maskButton) {
        
        _maskButton = [[UIButton alloc] initWithFrame:self.bounds];
        _maskButton.backgroundColor = [UIColor blackColor];
        _maskButton.alpha = 0.2;
        [_maskButton addTarget:self action:@selector(respondsToMask:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _maskButton;
}

@end













